<!--
 * @Descripttion:
 * @revision:
 * @Author: YouHuJun
 * @Date: 2021-09-22 14:51:37
 * @LastEditors: youhujun 2900976495@qq.com
 * @LastEditTime: 2025-03-05 14:37:32
-->
<template>
  <div>
    <el-form
      ref="updateVoiceConfigForm"
      :model="updateVoiceConfigForm"
      :rules="updateVoiceConfigRules"
      label-width="150px"
      :inline="true"
      label-position="left"
      status-icon
      inline-message
      style="width:80%;margin-left:auto;margin-right: auto;padding:30px 20px;"
    >
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="语音标题" prop="voice_title">
            <el-input v-model="updateVoiceConfigForm.voice_title" placeholder="语音标题" class="input" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-col :span="24">
            <el-form-item label="频道名称">
              <el-input v-model="updateVoiceConfigForm.channle_name" placeholder="频道名称" class="input" />
            </el-form-item>
          </el-col>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="频道事件">
            <el-input v-model="updateVoiceConfigForm.channle_event" placeholder="频道事件" class="input" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="语音保存类型">
            <el-select v-model="updateVoiceConfigForm.voice_save_type" placeholder="请选择">
              <el-option
                v-for="item in voiceSaveOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="updateVoiceConfigForm.voice_save_type == 10" type="flex">
        <el-col :span="8" class="center">
          <el-form-item
            label="语音地址"
            prop="voice_path"
          >
            <UploadFile
              :file-path.sync="updateVoiceConfigForm.voice_path"
              file-name="file"
              action-url="uploadConfigFile"
              :upload-data="{type:null, use_type:10, file_type: '', save_path: 'voice'}"
              type="audio"
              :accept="'audio/*'"
              show-content="上传语音"
            />
          </el-form-item>
        </el-col>
        <el-col v-show="updateVoiceConfigForm.voice_path" :span="16" style="vertical-align: middle;line-height:40px;">
          <span>{{ `路径：/${updateVoiceConfigForm.voice_path}` }}</span>
          <!-- <span class="upload-submit">替换上传</span> -->
        </el-col>
      </el-row>
      <el-row v-if="updateVoiceConfigForm.voice_save_type == 20" type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="语音地址" prop="voice_url">
            <el-input v-model="updateVoiceConfigForm.voice_url" placeholder="请填写语音地址" class="url-input" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="备注" prop="note">
            <el-input v-model="updateVoiceConfigForm.note" placeholder="备注" class="input" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="16">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="updateVoiceConfigForm.sort" class="input" />
          </el-form-item>
        </el-col>
        <el-col :span="8" style="vertical-align: middle;line-height:40px;">数值越大,顺序靠前,默认100</el-col>
      </el-row>
      <el-row type="flex" justify="center" style="margin-top:30px">
        <el-col :span="12" :offset="4">
          <el-form-item>
            <el-button type="primary" @click="submitForm('updateVoiceConfigForm')">修改配置项</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="4">
          <el-form-item>
            <el-button @click="resetForm('updateVoiceConfigForm')">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { updateVoiceConfig } from '@/api/laravel-fast-api/v1/system/systemConfig/voiceConfig'
import { debounce } from '@/utils'
import UploadFile from '@/pages/component/laravel-fast-api/v1/upload/uploadFile'

export default
{
  // 组件名称
  name: 'UpdateVoiceConfigForm',
  // 组件
  components:
    {
      UploadFile
    },
  props:
    {
      beforeVoiceConfigForm: {
        type: Object,
        default: null
      }
    },
  // 数据
  data()
  {
    return {
      // 添加菜单表单
      updateVoiceConfigForm: {
        id: 0,
        // 语音标题
        voice_title: '',
        // 频道名称
        channle_name: '',
        // 频道事件
        channle_event: '',
        // 语音保存类型
        voice_save_type: 10,
        // 语音地址 voice_save_type == 20 时必填
        voice_url: '',
        // 语音路径 voice_save_type == 10 时必填
        voice_path: '',
        // 备注
        note: '',
        // 排序
        sort: 100

      },
      // 添加表单规则
      updateVoiceConfigRules: {
        voice_title: [
          { required: true, message: '请输入语音标题', trigger: 'blur' }
        ],
        channle_name: [
          { required: true, message: '请输入频道名称', trigger: 'blur' }
        ],
        channle_event: [
          { required: true, message: '请输入频道事件', trigger: 'blur' }
        ],
        sort: [
          { pattern: /^[0-9]{1,10}$/, message: '只能0-9的数字', trigger: 'blur' }
        ]
      },
      // 音频保存选项
      voiceSaveOptions: [
        { value: 10, label: '本地保存' },
        { value: 20, label: 'url链接' }
      ]
    }
  },
  // 计算属性
  computed:
  {
  },
  // 监听
  watch:
  {
    beforeVoiceConfigForm: {
      handler(value)
      {
        this.updateVoiceConfigForm['id'] = value.id
        this.updateVoiceConfigForm['sort'] = value.sort
        this.updateVoiceConfigForm['voice_title'] = value.voice_title
        this.updateVoiceConfigForm['channle_name'] = value.channle_name
        this.updateVoiceConfigForm['channle_event'] = value.channle_event
        this.updateVoiceConfigForm['voice_url'] = value.voice_url
        this.updateVoiceConfigForm['voice_path'] = value.voice_path
        this.updateVoiceConfigForm['voice_save_type'] = value.voice_save_type
        this.updateVoiceConfigForm['note'] = value.note
      },
      deep: true,
      immediate: true
    }
  },
  // 实例创建之前
  beforeCreate()
  {

  },
  // 创建
  created()
  {

  },
  // 挂载之前
  beforeMount()
  {

  },
  // 挂载 --常用
  mounted()
  {

  },
  // 更新之前
  beforeUpdate()
  {
  },
  // 跟新后
  updated()
  {
  },
  // 销毁之前
  beforeDestroy()
  {
  },
  // 销毁后
  destroyed()
  {
  },
  // 方法
  methods:
  {
    initVoiceConfigForm()
    {
      this.$refs.updateVoiceConfigForm.resetFields()
    },
    // 提交表单
    submitForm: debounce(function(formName)
    {
      this.$refs[formName].validate((valid) =>
      {
        if (valid)
        {
          this.updateVoiceConfig(this.updateVoiceConfigForm)
        }
        else
        {
          console.log('未通过验证!')
          return false
        }
      })
    }, 600),

    // 重置表单
    resetForm(formName)
    {
      this.initVoiceConfigForm()
    },

    /**
     * 更新菜单
     */
    updateVoiceConfig(data)
    {
      const that = this
      updateVoiceConfig(data).then(res =>
      {
        if (res && res.code === 0)
        {
          that.$emit('handleCloseDialog', false)

          that.$message(
            {
              message: res.msg,
              type: 'success'
            })
          that.initVoiceConfigForm()
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
/**表单 */
.select,
.url-input {
  width: 500px;
}
.input {
  width: 300px;
}
.upload-submit {
  margin-left: 10px;
  font-size: 12px;
  color: #409eff;
  &:hover {
    text-decoration:underline
  }
}
</style>
